package com.zitangkou.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zitangkou.common.enums.RespEnum;
import com.zitangkou.common.global.GlobalResp;
import com.zitangkou.user.info.RoleInfo;
import com.zitangkou.user.req.role.AddRoleReq;
import com.zitangkou.user.req.role.EditRoleReq;
import com.zitangkou.user.req.role.GetRoleByPageReq;
import com.zitangkou.user.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户角色模块
 *
 * @author Deng Ningning
 * @version 1.0
 * @date 2020/8/31 16:39
 */

@RestController
@RequestMapping("/api/v1/roles")
public class RoleController {

    private final IRoleService roleService;

    @Autowired
    public RoleController(IRoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 分页查询用户角色
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.common.global.PageResp < com.zitangkou.user.model.entity.UserRole>>
     * @author Deng Ningning
     * @date 2020/9/1 17:24
     */
    @PostMapping("/page")
    public GlobalResp<IPage<RoleInfo>> getRolePage(
            @RequestBody @Validated GetRoleByPageReq req) {
        return GlobalResp.success(roleService.getRolePage(req));
    }

    /**
     * 添加用户角色
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:51
     */
    @PostMapping("/add")
    public GlobalResp<String> addRole(
            @RequestBody @Validated AddRoleReq req) {
        return roleService.insertRole(req)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.ADD_DEPARTMENT_FAIL);
    }

    /**
     * 修改用户角色
     *
     * @param req
     * @return top.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:53
     */
    @PutMapping("/edit")
    public GlobalResp<String> editRole(
            @RequestBody @Validated EditRoleReq req) {
        return roleService.updateRole(req)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.MODIFY_DEPARTMENT_FAIL);
    }

    /**
     * 删除用户角色
     *
     * @param roleId
     * @return top.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:55
     */
    @DeleteMapping("/remove/{roleId}")
    public GlobalResp<String> removeRole(
            @PathVariable String roleId) {
        return roleService.deleteRole(roleId)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.DELETE_DEPARTMENT_FAIL);
    }

    /**
     * 查询用户角色详情
     *
     * @param roleId
     * @return top.zitangkou.common.global.GlobalResp<top.zitangkou.user.model.entity.UserRole>
     * @author Deng Ningning
     * @date 2020/9/2 11:56
     */
    @GetMapping("/get/{roleId}")
    public GlobalResp<RoleInfo> getRole(
            @PathVariable String roleId) {
        return GlobalResp.success(roleService.selectById(roleId));
    }

}
